home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4517 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  45 lines

  1. Path: tph100.physik.uni-leipzig.de!not-for-mail
  2. From: wfeldt@physik.uni-leipzig.de (Steffen Winterfeldt)
  3. Newsgroups: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
  4. Subject: Re: float != float and floats as return types
  5. Followup-To: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
  6. Date: 30 Jan 1996 18:21:39 GMT
  7. Organization: Uni Leipzig
  8. Message-ID: <4elnjj$er4@server2.rz.uni-leipzig.de>
  9. References: <4ej9lb$mpc@fu-berlin.de>
  10. NNTP-Posting-Host: tph100.physik.uni-leipzig.de
  11. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  12.  
  13. Axel Thimm (axl@zedat.fu-berlin.de) wrote:
  14. : Hello,
  15. : I am getting confused, about how C/C++ manage float binary operations,
  16. : in particular multiplication. The next C++ example gives me surprising
  17. : results:
  18. :     *** cut here: begin file t_prec.cc
  19. :     #include <iostream.h>
  20. :     #include <iomanip.h>
  21. :     #include <math.h>
  22. :     float quad( float );
  23. :     int main() {
  24. :       for( int i=0; i<10; ++i ) {
  25. :         float a, b, c;
  26. :         a = i/13.123123;
  27. :         b = a*a;
  28. :         c = quad(a);
  29. :         cout << (b - c) << '\t';
  30. :         cout << (b - a*a) << '\t';
  31. :         cout << (c - quad(a)) << '\n';
  32. :       }
  33. :       return 0;
  34. :     }
  35. :     float quad( float x ) { return x*x; }
  36. : [...]
  37.  
  38. (c - quad(a)) is not zero, because quad's return value is in a floating point
  39. register and so has higher precision than c.
  40.  
  41. BTW, with higher optimization (say, -O3), even the second column becomes zero.
  42.  
  43.  
  44. Steffen
  45.